package B刷题.暴力递归.A回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wei.zhao
 * @description: 46.全排列
 * @date 2022年04月07日 17:19
 */
public class Code01_AllPermute {

    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        if (nums.length < 1) {
            return result;
        }
        boolean[] booleans = new boolean[nums.length];
        process(new ArrayList<>(), nums, booleans);
        return result;
    }

    private void process(ArrayList<Integer> list, int[] nums, boolean[] booleans) {
        if (list.size() == nums.length) {
            ArrayList<Integer> oneResult = new ArrayList<>();
            oneResult.addAll(list);
            result.add(oneResult);
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (booleans[i]) {
                continue;
            }
            booleans[i] = true;
            list.add(nums[i]);
            process(list, nums, booleans);
            booleans[i] = false;
            list.remove(list.size() - 1);
        }
    }

}
